<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>备授课 - <span th:text="${course.courseName}"></span></title>
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.4/css/bootstrap.min.css">
    <link rel="stylesheet" href="http://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script>
    <script src="http://cdn.bootcss.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background-color: #f5f5f5;
        }
        .navbar {
            border-radius: 0;
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
            border: none;
        }
        .navbar-brand {
            color: white !important;
            font-weight: bold;
        }
        .navbar-nav > li > a {
            color: rgba(255,255,255,0.9) !important;
        }
        .main-content {
            margin-top: 20px;
        }
        .sidebar {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        .sidebar h4 {
            color: #667eea;
            margin-top: 0;
        }
        .resource-type {
            margin-bottom: 15px;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 4px;
        }
        .resource-type h5 {
            margin-top: 0;
            color: #333;
        }
        .resource-item {
            padding: 8px;
            margin: 5px 0;
            background: white;
            border-left: 3px solid #667eea;
            border-radius: 4px;
        }
        .upload-area {
            border: 2px dashed #667eea;
            border-radius: 8px;
            padding: 30px;
            text-align: center;
            background: #f8f9fa;
            cursor: pointer;
            transition: all 0.3s;
        }
        .upload-area:hover {
            background: #e9ecef;
            border-color: #764ba2;
        }
        .upload-area.dragover {
            background: #e7f3ff;
            border-color: #667eea;
        }
        .btn-primary {
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
            border: none;
        }
    </style>
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <div class="navbar-header">
                <a class="navbar-brand" th:href="@{/teacher}" style="display: flex; align-items: center;">
                    <div class="avatar-container" style="display: inline-block; margin-right: 10px; vertical-align: middle;">
                        <img th:src="${session.teacher?.avatar != null and !session.teacher.avatar.isEmpty() ? '/' + session.teacher.avatar : ''}" 
                             style="width: 40px; height: 40px; border-radius: 50%; border: 2px solid rgba(255,255,255,0.8); object-fit: cover; cursor: pointer; transition: transform 0.2s;"
                             th:onclick="'location.href=\'' + @{/teacher/settings} + '\''"
                             onerror="if(this.src.indexOf('data:image') === -1) { this.onerror=null; this.src=''; }"
                             alt="头像">
                    </div>
                    <span><i class="fa fa-graduation-cap"></i> 析码无界 - 教师管理平台</span>
                </a>
            </div>
            <ul class="nav navbar-nav">
                <li><a th:href="@{/teacher/profile}">个人信息</a></li>
                <li><a th:href="@{/teacher/settings}"><i class="fa fa-cog"></i> 设置</a></li>
                <li class="active"><a th:href="@{/course/list}"><i class="fa fa-book"></i> 备授课</a></li>
                <li><a th:href="@{/assignment/list}">作业管理</a></li>
                <li><a th:href="@{/class/list}">班级管理</a></li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <li><a href="/"><i class="fa fa-sign-out"></i> 退出登录</a></li>
            </ul>
        </div>
    </nav>

    <div class="container main-content">
        <div th:if="${message}" class="alert alert-success" th:text="${message}"></div>
        <div th:if="${error}" class="alert alert-danger" th:text="${error}"></div>
        
        <div class="row">
            <!-- 左侧边栏 -->
            <div class="col-md-3">
                <div class="sidebar">
                    <h4><i class="fa fa-book"></i> <span th:text="${course.courseName}"></span></h4>
                    <p class="text-muted" th:if="${course.semester != null}">
                        <span th:text="${course.semester}"></span>
                        <span th:if="${course.courseType != null}" th:text="' | ' + ${course.courseType}"></span>
                    </p>
                    <div th:if="${associatedClasses != null and !associatedClasses.isEmpty()}" style="margin-top: 10px;">
                        <small class="text-muted">
                            <strong>关联班级：</strong>
                            <span th:each="clazz, iterStat : ${associatedClasses}">
                                <span th:text="${clazz.className}"></span><span th:if="${!iterStat.last}">、</span>
                            </span>
                        </small>
                    </div>
                    
                    <h5 style="margin-top: 20px; margin-bottom: 15px;">课程目录</h5>
                    
                    <!-- PPT资源 -->
                    <div class="resource-type">
                        <h5><i class="fa fa-file-powerpoint-o"></i> PPT</h5>
                        <div th:if="${resourcesByType['PPT'] != null and !resourcesByType['PPT'].isEmpty()}">
                            <div th:each="resource : ${resourcesByType['PPT']}" class="resource-item">
                                <span th:text="${resource.resourceName}"></span>
                                <small class="text-muted" th:if="${resource.visible}"> 可见</small>
                                <small class="text-muted" th:if="${resource.downloadable}"> 可下载</small>
                                <div class="pull-right">
                                    <a th:href="@{/course/resource/view(resourceId=${resource.id}, courseId=${course.id})}" 
                                       target="_blank" 
                                       class="btn btn-xs btn-info" 
                                       title="在线查看PPT"
                                       th:if="${resource.visible}">
                                        <i class="fa fa-eye"></i> 查看
                                    </a>
                                    <a th:href="@{'/course_resources/' + ${#strings.replace(resource.filePath, 'uploads/course_resources/', '')}}" 
                                       class="btn btn-xs btn-primary" 
                                       download
                                       title="下载PPT"
                                       th:if="${resource.downloadable}">
                                        <i class="fa fa-download"></i> 下载
                                    </a>
                                    <a th:href="@{/course/resource/delete(resourceId=${resource.id}, courseId=${course.id})}" 
                                       class="btn btn-xs btn-danger" 
                                       style="margin-left: 5px;"
                                       title="删除PPT"
                                       onclick="return confirm('确定要删除这个资源吗？')">
                                        <i class="fa fa-trash"></i> 删除
                                    </a>
                                </div>
                            </div>
                        </div>
                        <small class="text-muted" th:if="${resourcesByType['PPT'] == null or resourcesByType['PPT'].isEmpty()}">暂无PPT</small>
                    </div>
                    
                    <!-- 源代码资源 -->
                    <div class="resource-type">
                        <h5><i class="fa fa-code"></i> 源代码</h5>
                        <div th:if="${resourcesByType['源代码'] != null and !resourcesByType['源代码'].isEmpty()}">
                            <div th:each="resource : ${resourcesByType['源代码']}" class="resource-item">
                                <span th:text="${resource.resourceName}"></span>
                                <a th:href="@{'/' + ${resource.filePath}}" class="btn btn-xs btn-primary pull-right" download>
                                    <i class="fa fa-download"></i>
                                </a>
                            </div>
                        </div>
                        <small class="text-muted" th:if="${resourcesByType['源代码'] == null or resourcesByType['源代码'].isEmpty()}">暂无源代码</small>
                    </div>
                    
                    <!-- 实践报告资源 -->
                    <div class="resource-type">
                        <h5><i class="fa fa-file-text"></i> 实践报告</h5>
                        <div th:if="${resourcesByType['实践报告'] != null and !resourcesByType['实践报告'].isEmpty()}">
                            <div th:each="resource : ${resourcesByType['实践报告']}" class="resource-item">
                                <span th:text="${resource.resourceName}"></span>
                                <a th:href="@{'/' + ${resource.filePath}}" class="btn btn-xs btn-primary pull-right" download>
                                    <i class="fa fa-download"></i>
                                </a>
                            </div>
                        </div>
                        <small class="text-muted" th:if="${resourcesByType['实践报告'] == null or resourcesByType['实践报告'].isEmpty()}">暂无实践报告</small>
                    </div>
                    
                    <!-- 软件包资源 -->
                    <div class="resource-type">
                        <h5><i class="fa fa-archive"></i> 软件包</h5>
                        <div th:if="${resourcesByType['软件包'] != null and !resourcesByType['软件包'].isEmpty()}">
                            <div th:each="resource : ${resourcesByType['软件包']}" class="resource-item">
                                <span th:text="${resource.resourceName}"></span>
                                <a th:href="@{'/' + ${resource.filePath}}" class="btn btn-xs btn-primary pull-right" download>
                                    <i class="fa fa-download"></i>
                                </a>
                            </div>
                        </div>
                        <small class="text-muted" th:if="${resourcesByType['软件包'] == null or resourcesByType['软件包'].isEmpty()}">暂无软件包</small>
                    </div>
                    
                    <div style="margin-top: 20px;">
                        <button class="btn btn-primary btn-block" data-toggle="modal" data-target="#uploadModal">
                            <i class="fa fa-plus"></i> 新增
                        </button>
                        <small class="text-muted" style="display: block; margin-top: 10px;">支持文件拖拽上传</small>
                    </div>
                </div>
            </div>
            
            <!-- 主内容区 -->
            <div class="col-md-9">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><i class="fa fa-sitemap"></i> 课程结构图</h3>
                    </div>
                    <div class="panel-body">
                        <div style="min-height: 400px; padding: 20px; background: white; border-radius: 4px;">
                            <p class="text-muted">课程资源结构可视化显示区域</p>
                            <div th:if="${resourcesByType['PPT'] != null and !resourcesByType['PPT'].isEmpty()}">
                                <div class="row">
                                    <div th:each="resource : ${resourcesByType['PPT']}" class="col-md-3" style="margin-bottom: 15px;">
                                        <div class="panel panel-default">
                                            <div class="panel-body text-center">
                                                <i class="fa fa-file-powerpoint-o fa-3x" style="color: #ff6b6b;"></i>
                                                <h5 th:text="${resource.resourceName}"></h5>
                                                <small class="text-muted">PPT</small>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div th:if="${resourcesByType['PPT'] == null or resourcesByType['PPT'].isEmpty()}" class="alert alert-info">
                                <i class="fa fa-info-circle"></i> 还没有上传任何PPT资源，请点击左侧"新增"按钮上传PPT。
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- 上传资源模态框 -->
    <div class="modal fade" id="uploadModal" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title"><i class="fa fa-upload"></i> 上传课程资源</h4>
                </div>
                <form th:action="@{/course/resource/upload}" method="post" enctype="multipart/form-data" id="uploadForm">
                    <input type="hidden" name="courseId" th:value="${course.id}">
                    <div class="modal-body">
                        <div class="form-group">
                            <label>资源类型</label>
                            <select name="resourceType" class="form-control" required>
                                <option value="PPT">PPT</option>
                                <option value="源代码">源代码</option>
                                <option value="实践报告">实践报告</option>
                                <option value="软件包">软件包</option>
                                <option value="作业">作业</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label>资源名称（可选）</label>
                            <input type="text" name="resourceName" class="form-control" placeholder="留空则使用文件名">
                        </div>
                        <div class="form-group">
                            <label>章节编号（可选）</label>
                            <input type="number" name="chapterNumber" class="form-control" placeholder="如：1">
                        </div>
                        <div class="form-group">
                            <label>章节名称（可选）</label>
                            <input type="text" name="chapterName" class="form-control" placeholder="如：第一章">
                        </div>
                        <div class="form-group">
                            <label>选择文件</label>
                            <div class="upload-area" id="uploadArea">
                                <i class="fa fa-cloud-upload fa-3x" style="color: #667eea;"></i>
                                <p>点击或拖拽文件到此处上传</p>
                                <input type="file" name="file" id="fileInput" style="display: none;" required>
                            </div>
                            <div id="fileName" style="margin-top: 10px; font-weight: bold;"></div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button type="submit" class="btn btn-primary">上传</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    
    <script>
        // 文件选择
        document.getElementById('uploadArea').addEventListener('click', function() {
            document.getElementById('fileInput').click();
        });
        
        document.getElementById('fileInput').addEventListener('change', function(e) {
            if (e.target.files.length > 0) {
                document.getElementById('fileName').textContent = '已选择: ' + e.target.files[0].name;
            }
        });
        
        // 拖拽上传
        var uploadArea = document.getElementById('uploadArea');
        uploadArea.addEventListener('dragover', function(e) {
            e.preventDefault();
            uploadArea.classList.add('dragover');
        });
        
        uploadArea.addEventListener('dragleave', function(e) {
            e.preventDefault();
            uploadArea.classList.remove('dragover');
        });
        
        uploadArea.addEventListener('drop', function(e) {
            e.preventDefault();
            uploadArea.classList.remove('dragover');
            var files = e.dataTransfer.files;
            if (files.length > 0) {
                document.getElementById('fileInput').files = files;
                document.getElementById('fileName').textContent = '已选择: ' + files[0].name;
            }
        });
    </script>
</body>
</html>

